package com.cskaoyan.service.category;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.category.Category;
import com.cskaoyan.bean.category.CategoryExample;
import com.cskaoyan.bean.category.bo.CategoryCreateBoL2;
import com.cskaoyan.bean.category.bo.CategoryDeleteBoL1;
import com.cskaoyan.bean.category.vo.*;
import com.cskaoyan.bean.issue.Issue;
import com.cskaoyan.bean.issue.IssueExample;
import com.cskaoyan.bean.order.Comment;
import com.cskaoyan.bean.order.CommentExample;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.order.Order;
import com.cskaoyan.bean.wx.detail.vo.*;
import com.cskaoyan.bean.wx.goods.bo.GoodsListBo;
import com.cskaoyan.bean.wx.goods.vo.*;
import com.cskaoyan.bean.wx.home.Attribute;
import com.cskaoyan.bean.wx.home.AttributeExample;
import com.cskaoyan.bean.wx.home.WXBrand;
import com.cskaoyan.bean.wx.home.WXBrandExample;
import com.cskaoyan.bean.wx.home.vo.ChannelVo;
import com.cskaoyan.bean.wx.home.vo.GoodsVo;
import com.cskaoyan.bean.wx.home.vo.HotGoodsVo;
import com.cskaoyan.mapper.*;
import com.cskaoyan.mapper.home.AttributeMapper;
import com.cskaoyan.mapper.home.WXBrandMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;

/**
 * 创建日期: 2022/05/05 19:47
 *
 * @author JinLu Zhou
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    AttributeMapper attributeMapper;
    @Autowired
    WXBrandMapper wxBrandMapper;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    IssueMapper issueMapper;
    @Autowired
    MarketGoodsProductMapper productMapper;
    @Autowired
    MarketGoodsSpecificationMapper specificationMapper;

    @Override
    public BaseRespVo selectAllCategory() {
        List<Category> categoryList = categoryMapper.selectAllCategoryL1();
        ArrayList<L2Vo1> l2Vos = new ArrayList<>();
        for (Category category : categoryList) {
            L2Vo1 l2Vo = new L2Vo1();
            l2Vo.setCategory(category);

            Integer id = category.getId();
            CategoryExample categoryExample = new CategoryExample();
            CategoryExample.Criteria criteria = categoryExample.createCriteria();

            criteria.andPidEqualTo(id);
            criteria.andDeletedEqualTo(false);

            List<Category> categories = categoryMapper.selectByExample(categoryExample);
            ArrayList<L2Vo2> l2Vos2 = new ArrayList<>();
            for (Category category1 : categories) {
                L2Vo2 l2Vo2 = new L2Vo2();
                l2Vo2.setCategory(category1);
                l2Vos2.add(l2Vo2);
            }
            l2Vo.setChildren(l2Vos2);
            l2Vos.add(l2Vo);
        }
        BaseRespVo<CategoryDataVo<L2Vo1>> categoryDataVoBaseRespVo = new BaseRespVo<>();
        categoryDataVoBaseRespVo.setErrno(0);
        categoryDataVoBaseRespVo.setErrmsg("成功");


        CategoryDataVo<L2Vo1> l2Vo1CategoryDataVo = new CategoryDataVo<>();
        l2Vo1CategoryDataVo.setPagers(10);
        l2Vo1CategoryDataVo.setPage(1);
        l2Vo1CategoryDataVo.setLimit(10);
        Integer total = categoryMapper.selectAllCategoryNumber();
        l2Vo1CategoryDataVo.setTotal(total);
        l2Vo1CategoryDataVo.setList(l2Vos);
        categoryDataVoBaseRespVo.setData(l2Vo1CategoryDataVo);


        return categoryDataVoBaseRespVo;
    }

    @Override
    public Category createCategory(CategoryCreateBoL2 categoryCreateBo) {
        Category category = new Category();
        category.setDesc(categoryCreateBo.getDesc());
        category.setIconUrl(categoryCreateBo.getIconUrl());
        category.setKeywords(categoryCreateBo.getKeywords());
        category.setLevel(categoryCreateBo.getLevel());
        category.setName(categoryCreateBo.getName());
        category.setPicUrl(categoryCreateBo.getPicUrl());
        category.setPid(categoryCreateBo.getPid());
        category.setDeleted(false);
        category.setAddTime(new Date());
        category.setUpdateTime(new Date());

        //sort_order
        categoryMapper.insertSelective(category);

        return category;
    }

    @Override
    public BaseRespVo update(Category category) {
        BaseRespVo baseRespVo = new BaseRespVo();
        Integer id = category.getId();
        Category categoryTemp = categoryMapper.selectByPrimaryKey(id);
        String level = category.getLevel();

        CategoryExample categoryExample0 = new CategoryExample();
        CategoryExample.Criteria criteria0 = categoryExample0.createCriteria();
        criteria0.andPidEqualTo(id);
        //criteria0.andDeletedEqualTo(false);
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample0);
        //如果L1类目存在二级类目，则L1类目不能改成L2类目
        if ("L1".equals(categoryTemp.getLevel()) && "L2".equals(level)
                && (categoryList != null && categoryList.size() != 0)) {
            baseRespVo.setErrno(401);
            baseRespVo.setErrmsg("参数不对");
            return baseRespVo;
        }
        //将类目一改成类目二时一定要有父类目
        if ("L1".equals(categoryTemp.getLevel()) && "L2".equals(level) &&
                category.getPid() == null) {
            baseRespVo.setErrno(401);
            baseRespVo.setErrmsg("请选择父类目");
            return baseRespVo;
        }
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andIdEqualTo(id);
        category.setUpdateTime(new Date());
        categoryMapper.updateByExampleSelective(category, categoryExample);
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        return baseRespVo;
    }

    @Override
    @Transactional
    public BaseRespVo delete(CategoryDeleteBoL1 categoryDeleteBoL1) {
        Category category = new Category();
        category.setDeleted(true);

        CategoryExample categoryExample1 = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample1.createCriteria();
        criteria.andIdEqualTo(categoryDeleteBoL1.getId());
        categoryMapper.updateByExampleSelective(category, categoryExample1);

        CategoryExample categoryExample2 = new CategoryExample();
        CategoryExample.Criteria criteria2 = categoryExample2.createCriteria();
        criteria2.andPidEqualTo(categoryDeleteBoL1.getId());
        categoryMapper.updateByExampleSelective(category, categoryExample2);

        BaseRespVo baseRespVo = new BaseRespVo();
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        return baseRespVo;
    }

    @Override
    public List<ChannelVo> selectAllChannel() {
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andLevelEqualTo("L1");
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        ArrayList<ChannelVo> channelVos = new ArrayList<>();
        for (Category category : categoryList) {
            ChannelVo channelVo = new ChannelVo();
            channelVo.set(category);
            channelVos.add(channelVo);
        }
        return channelVos;
    }

    @Override
    public List<GoodsVo> queryFloorGoods(Integer id) {
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andPidEqualTo(id);
        ArrayList<GoodsVo> goodsVos = new ArrayList<>();
        for (Category category : categoryMapper.selectByExample(categoryExample)) {
            MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
            MarketGoodsExample.Criteria criteria1 = marketGoodsExample.createCriteria();
            criteria1.andDeletedEqualTo(false);
            criteria1.andIdEqualTo(category.getId());
            List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
            if (marketGoods != null && marketGoods.size() != 0) {
                GoodsVo goodsVo = new GoodsVo();
                goodsVo.set(marketGoods.get(0));
                goodsVos.add(goodsVo);
            }
        }
        return goodsVos;
    }

    @Override
    public BaseRespVo selectL2Category(Integer id) {
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andDeletedEqualTo(false);

        //作爲當前的GoodsCategory
        GoodsCategoryVo currentCategory = new GoodsCategoryVo();

        //id不知道是L1或L2的id
        criteria.andIdEqualTo(id);
        //必然是L1的id
        Integer id0 = id;
        List<Category> categoryList2 = categoryMapper.selectByExample(categoryExample);
        if (categoryList2.get(0).getPid() != 0) {
            //L2的id
            //currentCategory.set(categoryList2.get(0));
            categoryExample.clear();
            id0 = categoryList2.get(0).getPid();
        } else {
            categoryExample.clear();
            id0 = id;
        }

        //作爲父GoodsCategory
        categoryExample.clear();
        CategoryExample.Criteria criteria1 = categoryExample.createCriteria();
        criteria1.andDeletedEqualTo(false);
        criteria1.andIdEqualTo(id0);
        List<Category> categoryList1 = categoryMapper.selectByExample(categoryExample);
        GoodsCategoryVo parentCategory = new GoodsCategoryVo();
        parentCategory.set(categoryList1.get(0));

        //作爲兄弟類
        categoryExample.clear();
        CategoryExample.Criteria criteria2 = categoryExample.createCriteria();
        criteria2.andDeletedEqualTo(false);
        criteria2.andPidEqualTo(id0);
        List<Category> brotherCategory = categoryMapper.selectByExample(categoryExample);
        ArrayList<GoodsCategoryVo> brotherCategory2 = new ArrayList<>();
        for (Category category : brotherCategory) {
            GoodsCategoryVo categoryVo1 = new GoodsCategoryVo();
            categoryVo1.set(category);
            brotherCategory2.add(categoryVo1);
        }

        if (brotherCategory.size() == 0) {
            currentCategory = parentCategory;
        } else {
            if (id.equals(id0)) {
                currentCategory = brotherCategory2.get(0);
            } else {
                currentCategory.set(categoryList2.get(0));
            }
        }

        GoodsCategoryDataVo categoryDataVo = new GoodsCategoryDataVo();
        categoryDataVo.setCurrentCategory(currentCategory);
        categoryDataVo.setParentCategory(parentCategory);
        categoryDataVo.setBrotherCategory(brotherCategory2);

        BaseRespVo<GoodsCategoryDataVo> categoryDataVoBaseRespVo = new BaseRespVo<>();
        categoryDataVoBaseRespVo.setErrno(0);
        categoryDataVoBaseRespVo.setErrmsg("成功");
        categoryDataVoBaseRespVo.setData(categoryDataVo);
        return categoryDataVoBaseRespVo;
    }

    @Override
    public BaseRespVo selectGoodsList(GoodsListBo goodsListBo) {
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        if (goodsListBo.getCategoryId() != null) {
            criteria.andCategoryIdEqualTo(goodsListBo.getCategoryId());
        }
        if (goodsListBo.getBrandId() != null) {
            criteria.andBrandIdEqualTo(goodsListBo.getBrandId());
        }
        
        //分页
        PageHelper.startPage(goodsListBo.getPage(), goodsListBo.getLimit());
//执行查询过程中拼接分页信息
        List<GoodsListVo> goodsListVos = new ArrayList<>();

        List<MarketGoods> marketGoods1 = marketGoodsMapper.selectByExample(marketGoodsExample);
        for (MarketGoods marketGoods : marketGoods1) {
            GoodsListVo goodsListVo = new GoodsListVo();
            goodsListVo.set(marketGoods);
            goodsListVos.add(goodsListVo);
        }
        PageInfo<MarketGoods> pageInfo = new PageInfo<>(marketGoods1);
        long total = pageInfo.getTotal();
        int pages = (int) total / goodsListBo.getLimit() + 1;
        //查
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria1 = categoryExample.createCriteria();
        criteria1.andDeletedEqualTo(false);
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        criteria1.andLevelEqualTo("L1");
        List<Category> categoryList1 = categoryMapper.selectByExample(categoryExample);
        for (Category category : categoryList1) {
            CategoryExample categoryExample0 = new CategoryExample();
            CategoryExample.Criteria criteria2 = categoryExample0.createCriteria();
            criteria2.andDeletedEqualTo(false);
            criteria2.andPidEqualTo(category.getId());
            List<Category> categoryList2 = categoryMapper.selectByExample(categoryExample0);
            categoryList.removeAll(categoryList2);
        }
        categoryList.removeAll(categoryList1);

        List<FilterCategoryVo> filterCategoryVos = new ArrayList<>();
        for (Category category : categoryList) {
            FilterCategoryVo filterCategoryVo = new FilterCategoryVo();
            filterCategoryVo.set(category);
            filterCategoryVos.add(filterCategoryVo);
        }

        ListDataVo listDataVo = new ListDataVo();
        listDataVo.setLimit(goodsListBo.getLimit());
        listDataVo.setPage(goodsListBo.getPage());
        listDataVo.setPages(pages);
        listDataVo.setTotal((int) total);
        listDataVo.setList(goodsListVos);
        listDataVo.setFilterCategoryList(filterCategoryVos);

        BaseRespVo<ListDataVo> listDataVoBaseRespVo = new BaseRespVo<>();
        listDataVoBaseRespVo.setErrno(0);
        listDataVoBaseRespVo.setErrmsg("成功");
        listDataVoBaseRespVo.setData(listDataVo);
        return listDataVoBaseRespVo;
    }

    /*
     * 创建日期：2022/05/09 15:01
     ** @Param goodsId
     * 查詢商品的詳情
     * @author JinLu Zhou
     */
    @Override
    public BaseRespVo selectGoodsDetailList(Integer id) {

        //查詢商品的所有的attributes
        AttributeExample attributeExample = new AttributeExample();
        AttributeExample.Criteria attributeCriteria = attributeExample.createCriteria();
        attributeCriteria.andDeletedEqualTo(false);
        attributeCriteria.andGoodsIdEqualTo(id);
        List<Attribute> attributes = attributeMapper.selectByExample(attributeExample);
        ArrayList<AttributeVo> attributeVos = new ArrayList<>();//就是它  attributes
        for (Attribute attribute : attributes) {
            AttributeVo attributeVo = new AttributeVo();
            attributeVo.set(attribute);
            attributeVos.add(attributeVo);
        }
        //查詢商品的所有info
        //查詢商品的所有的brand
        MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(id);//info
        WXBrandExample wxBrandExample = new WXBrandExample();
        WXBrandExample.Criteria wxBrandCriteria = wxBrandExample.createCriteria();
        wxBrandCriteria.andDeletedEqualTo(false);
        wxBrandCriteria.andIdEqualTo(marketGoods.getBrandId());
        List<WXBrand> wxBrands = wxBrandMapper.selectByExample(wxBrandExample);
        GoodsBrandVo goodsBrandVo = new GoodsBrandVo();//就是它  brand
        if (wxBrands.size() != 0) {
            goodsBrandVo.set(wxBrands.get(0));
        }


        //查詢所有的issue
        IssueExample issueExample = new IssueExample();
        IssueExample.Criteria issueCriteria = issueExample.createCriteria();
        issueCriteria.andDeletedEqualTo(false);
        List<Issue> issues = issueMapper.selectByExample(issueExample);//就是它  issue

        //查詢所有的product
        MarketGoodsProductExample marketGoodsProductExample = new MarketGoodsProductExample();
        MarketGoodsProductExample.Criteria productCriteria = marketGoodsProductExample.createCriteria();
        productCriteria.andDeletedEqualTo(false);
        productCriteria.andGoodsIdEqualTo(id);
        //就是他 product
        List<MarketGoodsProduct> marketGoodsProducts = productMapper.selectByExample(marketGoodsProductExample);

        //查詢所有的specification
        MarketGoodsSpecificationExample marketGoodsSpecificationExample = new MarketGoodsSpecificationExample();
        MarketGoodsSpecificationExample.Criteria SpecificationCriteria = marketGoodsSpecificationExample.createCriteria();
        SpecificationCriteria.andDeletedEqualTo(false);
        SpecificationCriteria.andGoodsIdEqualTo(id);
        List<MarketGoodsSpecification> marketGoodsSpecifications = specificationMapper.selectByExample(marketGoodsSpecificationExample);
        LinkedHashSet<String> strings = new LinkedHashSet<>();
        for (MarketGoodsSpecification marketGoodsSpecification : marketGoodsSpecifications) {
            strings.add(marketGoodsSpecification.getSpecification());
        }
        ArrayList<SpecificationVo> specificationVos = new ArrayList<>();
        for (String string : strings) {
            MarketGoodsSpecificationExample marketGoodsSpecificationExample1 = new MarketGoodsSpecificationExample();
            MarketGoodsSpecificationExample.Criteria criteria = marketGoodsSpecificationExample1.createCriteria();
            criteria.andDeletedEqualTo(false);
            criteria.andGoodsIdEqualTo(id);
            criteria.andSpecificationEqualTo(string);
            List<MarketGoodsSpecification> marketGoodsSpecifications1 = specificationMapper.selectByExample(marketGoodsSpecificationExample1);

            SpecificationVo specificationVo = new SpecificationVo();
            specificationVo.setName(string);
            specificationVo.setValueList(marketGoodsSpecifications1);
            specificationVos.add(specificationVo);
        }
        Integer userId = 1;
        int i = marketGoodsMapper.selectCollect(userId, id);
        //查詢是否有收藏該商品

        //查詢商品的所有的comment
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria commentCriteria = commentExample.createCriteria();
        commentCriteria.andDeletedEqualTo(false);
        commentCriteria.andValueIdEqualTo(id);

        PageHelper.startPage(1, 7);
//执行查询过程中拼接分页信息
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        PageInfo<Comment> pageInfo = new PageInfo<>(comments);
        long total = pageInfo.getTotal();

        ArrayList<GoodsCommentDataVo> goodsCommentDataVos = new ArrayList<>();
        for (Comment comment : comments) {
            GoodsCommentDataVo goodsCommentDataVo = new GoodsCommentDataVo();
            goodsCommentDataVo.set(comment);
            User user = userMapper.selectByPrimaryKey(comment.getUserId());
            goodsCommentDataVo.setAvatar(user.getAvatar());
            goodsCommentDataVo.setNickname(user.getNickname());
            goodsCommentDataVos.add(goodsCommentDataVo);
        }
        GoodsCommentVo goodsCommentVo = new GoodsCommentVo();//就是它  comment
        goodsCommentVo.setData(goodsCommentDataVos);
        goodsCommentVo.setCount(total);


        //查詢所有的groupon
        GoodsDetailVo goodsDetailVo = new GoodsDetailVo();
        goodsDetailVo.setAttribute(attributeVos);
        goodsDetailVo.setBrand(goodsBrandVo);
        goodsDetailVo.setComment(goodsCommentVo);
        goodsDetailVo.setInfo(marketGoods);
        goodsDetailVo.setIssue(issues);
        goodsDetailVo.setProductList(marketGoodsProducts);
        goodsDetailVo.setShareImage("");
        goodsDetailVo.setSpecificationList(specificationVos);
        goodsDetailVo.setUserHasCollect(i);

        BaseRespVo<GoodsDetailVo> goodsDetailVoBaseRespVo = new BaseRespVo<>();
        goodsDetailVoBaseRespVo.setData(goodsDetailVo);
        goodsDetailVoBaseRespVo.setErrmsg("成功");
        goodsDetailVoBaseRespVo.setErrno(0);
        return goodsDetailVoBaseRespVo;
    }

    @Override
    public BaseRespVo selectRelated(Integer id) {
        MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(id);
        Category category = categoryMapper.selectByPrimaryKey(marketGoods.getCategoryId());

        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andCategoryIdEqualTo(category.getId());
        criteria.andIdNotEqualTo(id);

        PageHelper.startPage(1, 6);
        List<MarketGoods> marketGoods1 = marketGoodsMapper.selectByExample(marketGoodsExample);
        List<HotGoodsVo> hotGoodsVoList = new ArrayList<>();
        for (MarketGoods goods : marketGoods1) {
            HotGoodsVo hotGoodsVo = new HotGoodsVo();
            hotGoodsVo.set(goods);
            hotGoodsVoList.add(hotGoodsVo);
        }


        //执行查询过程中拼接分页信息
        PageInfo<MarketGoods> pageInfo = new PageInfo<>(marketGoods1);
        long total = pageInfo.getTotal();
        int pages = (int) total / 6 + 1;

        RelatedVo relatedVo = new RelatedVo();
        relatedVo.setLimit(6);
        relatedVo.setPage(1);
        relatedVo.setPages(pages);
        relatedVo.setTotal((int) total);
        relatedVo.setList(hotGoodsVoList);

        BaseRespVo<RelatedVo> relatedVoBaseRespVo = new BaseRespVo<>();
        relatedVoBaseRespVo.setData(relatedVo);
        relatedVoBaseRespVo.setErrmsg("成功");
        relatedVoBaseRespVo.setErrno(0);

        return relatedVoBaseRespVo;
    }

    @Override
    public void insertFootprint(Integer principal, Integer id) {
        categoryMapper.insertFootprint(principal,id);
    }

    @Override
    public Integer selectAllCategoryNumber() {
        return categoryMapper.selectAllCategoryNumber();
    }

    @Override
    public List<L1Vo> selectAllCategoryIdAndName() {
        return categoryMapper.selectAllCategoryIdAndName();
    }
}
